package org.particlethink.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.particlethink.dto.OrderStatusAndCount;
import org.particlethink.entry.OrderStatus;
import org.particlethink.model.Order;

import java.util.Date;
import java.util.List;

@Mapper
public interface OrderMapper extends tk.mybatis.mapper.common.Mapper<Order> {
    @Select("select * from orders limit  #{limit} offset #{offset}")
    List<Order> selectByPagination(@Param("offset") int offset, @Param("limit") int limit);

    @Select("<script>" +
            "select * from orders where batch_plan_id =  #{batchPlanID} and status in " +
            "<foreach item='item' index='index' collection='statuses' open='(' separator=', ' close=')'>" +
            "#{item.code}" +
            "</foreach>" +
            "</script>")
    List<Order> selectByBatchPlanIdAndStatuses(@Param("batchPlanID") int batchPlanID, @Param("statuses") List<OrderStatus> statuses);

    List<Order> selectByBatchPlanIdsAndStatuses(@Param("batchPlanIDs") List<Integer> batchPlanIDs, @Param("statuses") List<OrderStatus> statuses);

    @Delete("delete from orders where batch_plan_id =  #{batchPlanID}")
    void deleteByBatchPlanId(@Param("batchPlanID") int batchPlanID);


    @Select("select * from orders where batch_plan_id =  #{batchPlanID}")
    List<Order> selectByBatchPlanId(@Param("batchPlanID") int batchPlanID);


    @Select("select * from orders where supplier_id =  #{supplierId} and status = #{status.code} limit  #{limit} offset #{offset}")
    List<Order> selectBySupplierIdAndStatus(@Param("supplierId") int supplierId, @Param("status") OrderStatus status, @Param("offset") int offset, @Param("limit") int limit);

    List<Order> queryOrders(
            @Param("projectIds") List<Integer> projectIds,
            @Param("supplierIds") List<Integer> supplierIds,
            @Param("no") String no,
            @Param("batchNo") String batchNo,
            @Param("masterBatchNo") String masterBatchNo,
            @Param("categoryIds") List<String> categoryIds,
            @Param("creatorIds") List<Integer> creatorIds,
            @Param("status") OrderStatus status,
            @Param("submitStart") Date submitStart,
            @Param("submitEnd") Date submitEnd,
            @Param("searchKey") String searchKey
    );

    List<Order> queryNo(@Param("status") OrderStatus status, @Param("no") String no);

    List<OrderStatusAndCount> countByStatus(@Param("supplierIds") List<Integer> supplierIds, @Param("projectIds") List<Integer> projectIds);

}
